package com.huawei.appgallery.foundation.ui.css.adapter.type.factory;

import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSColorList;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSMonoColor;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSValue;
import java.util.ArrayList;
import java.util.List;
import o.Logger;
import o.dof;
import org.json.JSONObject;

public class CSSColorFactory implements CSSValueFactory {
    private static List<Pair<String, Integer>> STATE_TABLE = new ArrayList<Pair<String, Integer>>() {
        private static final long serialVersionUID = 3836495972812820826L;

        {
            add(new Pair("pressed", Integer.valueOf(16842919)));
            add(new Pair("focused", Integer.valueOf(16842908)));
            add(new Pair("selected", Integer.valueOf(16842913)));
            add(new Pair("checkable", Integer.valueOf(16842911)));
            add(new Pair("checked", Integer.valueOf(16842912)));
            add(new Pair("enabled", Integer.valueOf(16842910)));
            add(new Pair("window_focused", Integer.valueOf(16842909)));
            add(new Pair("default", Integer.valueOf(-1)));
        }
    };
    private static final String TAG = "CSSColorFactory";

    public CSSValue create(String str) {
        if (dof.ᐝ(str)) {
            return parserColorList(str);
        }
        return parserColor(str);
    }

    protected CSSColorList parserColorList(String str) {
        try {
            JSONObject jSONObject = new JSONObject(str);
            CSSColorList cSSColorList = new CSSColorList();
            for (Pair pair : STATE_TABLE) {
                Object optString = jSONObject.optString((String) pair.first);
                if (!TextUtils.isEmpty(optString)) {
                    CSSMonoColor parserColor = parserColor(optString);
                    if (parserColor != null) {
                        cSSColorList.add(((Integer) pair.second).intValue(), parserColor);
                    }
                }
            }
            return cSSColorList;
        } catch (Throwable e) {
            Logger.ˋ(TAG, "parser colorList parameter failed from: " + str, e);
            return null;
        }
    }

    private CSSMonoColor parserColor(String str) {
        try {
            if (!TextUtils.isEmpty(str)) {
                return new CSSMonoColor(Color.parseColor(str));
            }
        } catch (IllegalArgumentException e) {
            Log.e(TAG, str + " Argument format error." + e.getMessage());
        }
        return null;
    }
}
